package com.bj58.mis.apm.server.statistics.mock.interceptor;

import com.bj58.mis.apm.server.core.mock.annotation.Mock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author: zhangfx
 * @create: 2018-06-20 18:13
 **/
public class MockInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${mock.basePath:/mock}")
    private String mockBasePath;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler instanceof HandlerMethod){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            String path = getPath(handlerMethod);
            if(path!=null){
                String mockPath = mockBasePath + path;
                logger.debug("[Interceptor]返回mock数据, requestpath={}, mockPath={}", request.getServletPath(), mockPath);
                request.getRequestDispatcher(mockPath).forward(request, response);
                return false;
            }
        }
        return true;
    }

    private String getPath(HandlerMethod handlerMethod){
        Method method = handlerMethod.getMethod();
        Class controllerClass = method.getDeclaringClass();
        Mock classMock = (Mock) controllerClass.getAnnotation(Mock.class);
        Mock methodMock = method.getAnnotation(Mock.class);
        if(classMock!=null && !classMock.enable()){
            return null;
        }
        if(methodMock!=null && !methodMock.enable()){
            return null;
        }
        String path = null;
        if(classMock!=null){
            if(classMock.path().length>0){
                path = classMock.path()[0];
            }else{
                path = "/" + controllerClass.getSimpleName();
            }
            if(methodMock!=null && methodMock.path().length>0){
                path += methodMock.path()[0];
            }else {
                path += "/" + method.getName() + ".json";
            }
        }else if (methodMock!=null){
            if(methodMock.path().length>0){
                path = methodMock.path()[0];
            }else {
                path = "/" + method.getName() + ".json";
            }
        }
        return path;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {

    }
}
